home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / basic / ace24dist.lha / ace24.lha / prgs / Misc / array_via_pointers.b < prev    next >
Text File  |  1996-09-11  |  963b  |  35 lines

  1.            '..Using pointers to implement an array of numbers  
  2.       '..(single-precision values in this case). 
  3.  
  4.       {* Variables *} 
  5.           ADDRESS memBlock 
  6.  
  7.       {* Subprograms *} 
  8.           SUB SetElement(ADDRESS addr, LONGINT i, SINGLE x) 
  9.             *!(addr + i*SIZEOF(SINGLE)) := x 
  10.           END SUB 
  11.  
  12.           SUB SINGLE Element(ADDRESS addr, LONGINT i) 
  13.             Element = *!(addr + i*SIZEOF(SINGLE)) 
  14.           END SUB 
  15.  
  16.       {* Main *} 
  17.  
  18.           '..Allocate a memBlock of N single-precision floats
  19.           INPUT "How many numbers? ",N 
  20.           memBlock = ALLOC(N*SIZEOF(SINGLE)) 
  21.           IF memBlock = 0& THEN STOP 
  22.           PRINT "Allocated";N;"values ( 0 to";N-1;")" 
  23.  
  24.           '..Fill "array" with the squares of i 
  25.           FOR i=0 TO N-1
  26.             SetElement(memBlock, i, i*i) 
  27.           NEXT 
  28.  
  29.           '..Display "array" elements 
  30.           FOR i=0 TO N-1
  31.             PRINT i, Element(memBlock, i) 
  32.           NEXT 
  33.  
  34.       END
  35.